package l1j.server.server.clientpackets;

import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.lock.ClanEmblemReading;
import l1j.server.server.datatables.lock.ClanReading;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Emblem;
import l1j.server.server.templates.L1EmblemIcon;

import java.util.logging.Level;
import java.util.logging.Logger;
// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來的要求血盟徽章封包
 */
public class C_Clan extends ClientBasePacket {

	private static final String C_CLAN = "[C] C_Clan";
	private static final Logger _log = Logger.getLogger(C_Clan.class.getName());
	@Override
	public String getType() {
		return C_CLAN;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

		final L1PcInstance pc = client.getActiveChar();
		if (pc == null) {
			return;
		}

		if (pc.isGhost()) { // 鬼魂模式
			return;
		}

		if (pc.isDead()) { // 死亡
			return;
		}

		if (pc.isTeleport()) { // 傳送中
			return;
		}

		final int clanId = this.readD();

		final L1Clan clan = ClanReading.get().getTemplate(clanId);

		if(clan == null){
			return;
		}

		final L1EmblemIcon emblemIcon = ClanEmblemReading.get().get(clan.getClanId());

		if (emblemIcon != null) {
			pc.sendPackets(new S_Emblem(emblemIcon));
		}

		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}

}
